package com.tiger.admin.http.common.platformConfig;

import com.tiger.admin.Db;
import com.tiger.db.entity.PlatformConfig;
import com.tiger.utils.ResponseUtil;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/platformConfig")
public class PlatformConfigController {
    private final Db db;

    public PlatformConfigController(Db db) {
        this.db = db;
    }

    @GetMapping()
    public Object config() {
        PlatformConfig config = db.platformConfig.getConfig();
        return ResponseUtil.successRes(config);
    }

    @PostMapping("update")
    public Object update(@RequestBody PlatformConfig data) {
        PlatformConfig config = db.platformConfig.getConfig(PlatformConfig::getId);
        boolean r;
        if(config == null) {
            data.setId(null);
            r = db.platformConfig.save(data);
        }else {
            data.setId(config.getId());
            r = db.platformConfig.updateById(data);
        }



        return r ? ResponseUtil.successRes(data) : ResponseUtil.failRes();
    }
}
